using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Net.Mail;
using Decav.Gatsb.Configuration;

namespace Decav.Gatsb.Application.CommandWorkflow
{
    /// <summary>
    /// Sends email.
    /// </summary>
	public partial class SendMailActivity: Activity
	{
		public SendMailActivity()
		{
			InitializeComponent();
		}

        /// <summary>
        /// The dependency property that gets or sets the body of the message to send.
        /// </summary>
        public static DependencyProperty MessageBodyProperty = DependencyProperty.Register(
            "MessageBody", typeof(string), typeof(SendMailActivity));

        /// <summary>
        /// Gets or sets the body of the message to send.
        /// </summary>
        [ValidationOption(ValidationOption.Required)]
        public string MessageBody
        {
            get { return (string)GetValue(MessageBodyProperty); }
            set { SetValue(MessageBodyProperty, value); }
        }

        /// <summary>
        /// The dependency property that gets or sets the sending address.
        /// </summary>
        public static DependencyProperty SendingAddressProperty = DependencyProperty.Register(
            "SendingAddress", typeof(string), typeof(SendMailActivity));

        /// <summary>
        /// Gets or sets the sending address.
        /// </summary>
        [ValidationOption(ValidationOption.Required)]
        public string SendingAddress
        {
            get { return (string)GetValue(SendingAddressProperty); }
            set { SetValue(SendingAddressProperty, value); }
        }

        /// <summary>
        /// The dependency property that gets or sets the recipient of the message.
        /// </summary>
        public static DependencyProperty RecipientAddressProperty = DependencyProperty.Register(
            "RecipientAddress", typeof(string), typeof(SendMailActivity));

        /// <summary>
        /// Gets or sets the recipient of the message.
        /// </summary>
        public string RecipientAddress
        {
            get { return (string)GetValue(RecipientAddressProperty); }
            set { SetValue(RecipientAddressProperty, value); }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            SendMail(RecipientAddress, SendingAddress, MessageBody);
            return ActivityExecutionStatus.Closed;
        }

        private void SendMail(string email, string sender, string message)
        {
            Console.WriteLine("Emailed {0} from {1}:\r\n{2}", email, sender, message);

#if !DEBUG
            SmtpClient client = new SmtpClient(GatsbConfigurationSection.Current.SmtpServer);
            client.Send(sender, email, null, message);
#endif
        }
	}
}
